package com.bill99.codecheck.sonar.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import org.apache.commons.io.FileUtils;

import com.bill99.codecheck.sonar.schema.ObjectFactory;
import com.bill99.codecheck.sonar.schema.Project;
import com.bill99.codecheck.sonar.schema.Property;
import com.bill99.codecheck.sonar.schema.Sonar;
import com.bill99.codecheck.sonar.schema.Target;
import com.bill99.codecheck.sonar.service.GenerateBuildXmlService;

public class GenerateBuildXmlServiceImpl implements GenerateBuildXmlService {

	private String projectBasedir;
	private String projectDefault;
	private String srcDirName;
	private String srcDirValue;
	private String projectKeyName;
	private String sonarProjectName;
	private String projectVersionName;
	private String projectVersionValue;
	private String sonarLanguageName;
	private String sonarLanguageValue;
	private String sonarSourcesName;
	private String sonarSourcesValue;
	private String sourceEncodingName;
	private String sourceEncodingValue;
	private String jdbcUrlName;
	private String jdbcUrlValue;
	private String driverClassName;
	private String driverClassNameValue;
	private String jdbcUserName;
	private String jdbcUserNameValue;
	private String jdbcPasswordName;
	private String jdbcPasswordValue;
	private String hostUrlName;
	private String hostUrlValue;
	private String sonarTargetName;
	private String allTargetName;
	private String allTargetDepends;

	public boolean generateXml(String targetName, String xmlPath) {
		try {
			JAXBContext jc = JAXBContext.newInstance("com.bill99.codecheck.sonar.schema");
			ObjectFactory objFactory = new ObjectFactory(); //生成对象工厂
			//Project标签生成
			Project project = objFactory.createProject();
			project.setBasedir(projectBasedir);
			project.setDefault(projectDefault);
			project.setName(targetName);
			List<Property> propertyList = new ArrayList<Property>();
			List<Target> targetList = new ArrayList<Target>();
			//property标签生成
			Property srcdirProperty = objFactory.createProperty();
			srcdirProperty.setName(srcDirName);
			srcdirProperty.setValue(srcDirValue);
			propertyList.add(srcdirProperty);

			Property projectKeyProperty = objFactory.createProperty();
			projectKeyProperty.setName(projectKeyName);
			projectKeyProperty.setValue(targetName);
			propertyList.add(projectKeyProperty);

			Property projectNameProperty = objFactory.createProperty();
			projectNameProperty.setName(sonarProjectName);
			projectNameProperty.setValue(targetName);
			propertyList.add(projectNameProperty);

			Property projectVersionProperty = objFactory.createProperty();
			projectVersionProperty.setName(projectVersionName);
			projectVersionProperty.setValue(projectVersionValue);
			propertyList.add(projectVersionProperty);

			Property languageProperty = objFactory.createProperty();
			languageProperty.setName(sonarLanguageName);
			languageProperty.setValue(sonarLanguageValue);
			propertyList.add(languageProperty);

			Property sonarSourcesProperty = objFactory.createProperty();
			sonarSourcesProperty.setName(sonarSourcesName);
			sonarSourcesProperty.setValue(sonarSourcesValue);
			propertyList.add(sonarSourcesProperty);

			Property sonarEncodingProperty = objFactory.createProperty();
			sonarEncodingProperty.setName(sourceEncodingName);
			sonarEncodingProperty.setValue(sourceEncodingValue);
			propertyList.add(sonarEncodingProperty);

			Property jdbcUrlProperty = objFactory.createProperty();
			jdbcUrlProperty.setName(jdbcUrlName);
			jdbcUrlProperty.setValue(jdbcUrlValue);
			propertyList.add(jdbcUrlProperty);

			Property driverClassNameProperty = objFactory.createProperty();
			driverClassNameProperty.setName(driverClassName);
			driverClassNameProperty.setValue(driverClassNameValue);
			propertyList.add(driverClassNameProperty);

			Property jdbcUsernameProperty = objFactory.createProperty();
			jdbcUsernameProperty.setName(jdbcUserName);
			jdbcUsernameProperty.setValue(jdbcUserNameValue);
			propertyList.add(jdbcUsernameProperty);

			Property jdbcPasswordProperty = objFactory.createProperty();
			jdbcPasswordProperty.setName(jdbcPasswordName);
			jdbcPasswordProperty.setValue(jdbcPasswordValue);
			propertyList.add(jdbcPasswordProperty);

			Property hostUrlProperty = objFactory.createProperty();
			hostUrlProperty.setName(hostUrlName);
			hostUrlProperty.setValue(hostUrlValue);
			propertyList.add(hostUrlProperty);

			//target标签生成
			Target sonarTarget = objFactory.createTarget();
			sonarTarget.setName(sonarTargetName);
			// sonar标签生成
			Sonar sonar = objFactory.createSonar();
			sonarTarget.setSonar(sonar);
			targetList.add(sonarTarget);
			//target标签生成
			Target allTarget = objFactory.createTarget();

			allTarget.setName(allTargetName);
			allTarget.setDepends(allTargetDepends);
			targetList.add(allTarget);

			project.property = propertyList;
			project.target = targetList;

			Marshaller marshaller = jc.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

			OutputStream bos = new ByteArrayOutputStream();
			marshaller.marshal(project, bos);
			System.out.println(bos.toString());
			System.out.println("xmlPath=" + xmlPath);
			File commitFile = new File(xmlPath);

			FileUtils.writeStringToFile(commitFile, bos.toString(), "utf-8");
			bos.close();

		} catch (Exception e) {
			return false;
		}

		return true;
	}

	public void setProjectBasedir(String projectBasedir) {
		this.projectBasedir = projectBasedir;
	}

	public void setProjectDefault(String projectDefault) {
		this.projectDefault = projectDefault;
	}

	public void setSrcDirName(String srcDirName) {
		this.srcDirName = srcDirName;
	}

	public void setSrcDirValue(String srcDirValue) {
		this.srcDirValue = srcDirValue;
	}

	public void setProjectKeyName(String projectKeyName) {
		this.projectKeyName = projectKeyName;
	}

	public void setSonarProjectName(String sonarProjectName) {
		this.sonarProjectName = sonarProjectName;
	}

	public void setProjectVersionName(String projectVersionName) {
		this.projectVersionName = projectVersionName;
	}

	public void setProjectVersionValue(String projectVersionValue) {
		this.projectVersionValue = projectVersionValue;
	}

	public void setSonarLanguageName(String sonarLanguageName) {
		this.sonarLanguageName = sonarLanguageName;
	}

	public void setSonarLanguageValue(String sonarLanguageValue) {
		this.sonarLanguageValue = sonarLanguageValue;
	}

	public void setSonarSourcesName(String sonarSourcesName) {
		this.sonarSourcesName = sonarSourcesName;
	}

	public void setSonarSourcesValue(String sonarSourcesValue) {
		this.sonarSourcesValue = sonarSourcesValue;
	}

	public void setSourceEncodingName(String sourceEncodingName) {
		this.sourceEncodingName = sourceEncodingName;
	}

	public void setSourceEncodingValue(String sourceEncodingValue) {
		this.sourceEncodingValue = sourceEncodingValue;
	}

	public void setJdbcUrlName(String jdbcUrlName) {
		this.jdbcUrlName = jdbcUrlName;
	}

	public void setJdbcUrlValue(String jdbcUrlValue) {
		this.jdbcUrlValue = jdbcUrlValue;
	}

	public void setDriverClassName(String driverClassName) {
		this.driverClassName = driverClassName;
	}

	public void setDriverClassNameValue(String driverClassNameValue) {
		this.driverClassNameValue = driverClassNameValue;
	}

	public void setJdbcUserName(String jdbcUserName) {
		this.jdbcUserName = jdbcUserName;
	}

	public void setJdbcUserNameValue(String jdbcUserNameValue) {
		this.jdbcUserNameValue = jdbcUserNameValue;
	}

	public void setJdbcPasswordName(String jdbcPasswordName) {
		this.jdbcPasswordName = jdbcPasswordName;
	}

	public void setJdbcPasswordValue(String jdbcPasswordValue) {
		this.jdbcPasswordValue = jdbcPasswordValue;
	}

	public void setHostUrlName(String hostUrlName) {
		this.hostUrlName = hostUrlName;
	}

	public void setHostUrlValue(String hostUrlValue) {
		this.hostUrlValue = hostUrlValue;
	}

	public void setSonarTargetName(String sonarTargetName) {
		this.sonarTargetName = sonarTargetName;
	}

	public void setAllTargetName(String allTargetName) {
		this.allTargetName = allTargetName;
	}

	public void setAllTargetDepends(String allTargetDepends) {
		this.allTargetDepends = allTargetDepends;
	}

}
